package com.controller;

import com.dto.BaseDto;
import com.entity.Clazz;
import com.page.PageInfo;
import com.servcice.ClazzService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.swing.text.html.Option;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@Controller
public class ClazzController extends BaseController{
    @Resource
    private ClazzService clazzService;

    @RequestMapping("/clazz/getAllJSon")
    @ResponseBody //将返回值以json格式输出
    public BaseDto<Clazz> getAllJSon() throws Exception{
        return clazzService.getAll();
    }

    /**
     * 前台查询分页显示，ajax方式
     * @param pageInfo
     * @return
     */
    @RequestMapping("/clazz/page")
    @ResponseBody //将返回值以json格式输出
    public BaseDto<Clazz> getClazzsByPage(PageInfo pageInfo) throws Exception{
        return clazzService.getClazzsByPage(pageInfo);
    }

    /**
     * 前台 根据姓名搜索学生
     * @param name
     * @return
     */
        @RequestMapping("/clazz/searchClazzByName/{name}")
        @ResponseBody //将返回值以json格式输出
        //@PathVariable 将url中以{xxx}形式传递的参数值 绑定给当前的方法某一个参数，就可以解决传递的参数名和方法定义的参数名 名称不同无法自动绑定的问题
        public BaseDto<Clazz> searchClazzByName(@PathVariable("name") String name, HttpServletRequest request) throws Exception{
            String currPage = request.getParameter("currPage");
            String pageSize = request.getParameter("pageSize");
            PageInfo pageInfo = new PageInfo();
            currPage = Optional.ofNullable(currPage).orElse(String.valueOf(pageInfo.getCurrPage()));
            pageSize = Optional.ofNullable(pageSize).orElse(String.valueOf(pageInfo.getPageSize()));

            pageInfo.setCurrPage(Integer.parseInt(currPage));
            pageInfo.setPageSize(Integer.parseInt(pageSize));
            return clazzService.searchClazzByName("%"+name+"%",pageInfo);
        }
    /**
     * 后台查询分页显示，跳转方式
     * @param pageInfo
     * @return
     */
    @RequestMapping("/clazz/back/page")
    public ModelAndView getClazzsBackByPage(PageInfo pageInfo) throws Exception{
        BaseDto<Clazz> clazzsDTO = clazzService.getClazzsByPage(pageInfo);
        return new ModelAndView("/back/clazz/list","dto",clazzsDTO);
    }

    /**
     * 前台根据学号（主键查询学生带选课信息）
     * @param sno
     * @return
     * @throws Exception
     */
    //restful风格  路径不能有动词  /clazz/back/clazz 表示按主键查询
    //                         /clazz/back/clazzs  表示查询全部
    //                         /clazz/back/pageClazzs 表示分页查询
    //                         /clazz/back/
    @RequestMapping(value = "/front/clazzInfo/{sno}")
    public ModelAndView selectClazzWithScsAndCourse(@PathVariable("sno") String sno) throws Exception{
        BaseDto<Clazz> clazzsDTO = clazzService.selectClazzWithScsAndCourse(sno);
        return new ModelAndView("/front/clazz/clazzInfo","dto",clazzsDTO);
    }

    @RequestMapping(value = "/front/clazzInfoJson/{sno}")
    @ResponseBody
    public BaseDto selectClazzWithScsAndCourse1(@PathVariable("sno") String sno) throws Exception{
        BaseDto<Clazz> clazzsDTO = clazzService.selectClazzWithScsAndCourse(sno);
        return clazzsDTO;
    }




    @RequestMapping("/clazz/getAll")
    public ModelAndView getAll(){
        return new ModelAndView("/clazz/getAll");
    }


    @RequestMapping("/clazz/clazzsJson")
    public BaseDto<Clazz> getAllJson(){

        return null;
    }



    @RequestMapping("/clazz/add")
    @ResponseBody
    public int addClazz(Clazz clazz) throws Exception{
        return clazzService.addClazz(clazz);
    }
}
